#!/bin/bash

# 定义 Hive 安装包路径和安装目录
HIVE_PACKAGE="/opt/software/apache-hive-3.1.3-bin.tar.gz"
HIVE_INSTALL_DIR="/opt/hive"

# 解压 Hive 安装包
echo "正在解压 Hive 安装包..."
mkdir -p $HIVE_INSTALL_DIR
tar -zxf $HIVE_PACKAGE -C $HIVE_INSTALL_DIR --strip-components=1

# 配置 Hive 环境变量
echo "正在配置 Hive 环境变量..."
echo "# Hive 环境变量" >> ~/.bashrc
echo "export HIVE_HOME=$HIVE_INSTALL_DIR" >> ~/.bashrc
echo "export PATH=\$PATH:\$HIVE_HOME/bin" >> ~/.bashrc
source ~/.bashrc

# 下载 MySQL JDBC 驱动
echo "正在下载 MySQL JDBC 驱动..."
wget -P $HIVE_INSTALL_DIR/lib https://repo1.maven.org/maven2/mysql/mysql-connector-java/8.0.26/mysql-connector-java-8.0.26.jar

# 配置 Hive 元数据存储为 MySQL
echo "正在配置 Hive 元数据存储为 MySQL..."
cd $HIVE_INSTALL_DIR/conf

# 直接创建 hive-site.xml 并写入内容
cat << EOF > hive-site.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
  <property>
    <name>javax.jdo.option.ConnectionURL</name>
    <value>jdbc:mysql://localhost:3306/hive_metastore?createDatabaseIfNotExist=true</value>
  </property>
  <property>
    <name>javax.jdo.option.ConnectionDriverName</name>
    <value>com.mysql.cj.jdbc.Driver</value>
  </property>
  <property>
    <name>javax.jdo.option.ConnectionUserName</name>
    <value>root</value>
  </property>
  <property>
    <name>javax.jdo.option.ConnectionPassword</name>
    <value>123456</value>
  </property>
</configuration>
EOF

echo  "启动mysqld数据库"
systemctl  start   mysqld
cp    $HADOOP_HOME/share/hadoop/common/lib/guava-27.0-jre.jar   $HIVE_HOME/lib
rm   -rf   $HIVE_HOME/lib/guava-19.0.jar

# 初始化 Hive 元数据存储
echo "正在初始化 Hive 元数据存储..."
schematool -initSchema -dbType mysql

echo "Hive 本地模式部署完成！"
    